package com.zh.netty.nio;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * 案例4
 * 使用FileChannel(通道)和 方法transferFrom ，完成文件的拷贝
 */
public class NIOFileChannel04 {

    public static void main(String[] args) throws IOException {
        // 创建输入流
        FileInputStream inputStream = new FileInputStream("d:\\a.jpg");
        // 创建输出流
        FileOutputStream outputStream = new FileOutputStream("d:\\b.jpg");

        // 创建相关通道
        FileChannel inputStreamChannel = inputStream.getChannel();
        FileChannel outputStreamChannel = outputStream.getChannel();

        // 使用transferFrom复制文件，从目标通道中复制数据到当前通道
        outputStreamChannel.transferFrom(inputStreamChannel,0,inputStreamChannel.size());

        // 关闭相关流
        inputStreamChannel.close();
        outputStream.close();
        inputStream.close();
        outputStream.close();
    }
}
